package com.example.base.fragment;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.example.base.R;
import com.example.base.adapter.FlowAdapter;
import com.example.base.adapter.FlowAdapter2;
import com.example.base.bean.NavOne;
import com.example.base.bean.NavTwo;
import com.example.base.bean.SystemOne;
import com.example.base.bean.SystemTwo;
import com.example.base.view.SysViewLayout;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class NavigateFragment extends Fragment {


  private TextView textView;
  private String mParam1;
  private String mParam2;
  private Context context;
  private SysViewLayout sysViewLayout;
  private FlowAdapter2 flowAdapter;
  private ListView listView;
  private List<NavTwo> dataList = new ArrayList<>();

  public NavigateFragment() {
  }


  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
    View inflate = inflater.inflate(R.layout.fragment_system, container, false);
    initView(inflate);
    initListener();
    initSystemView();
    sendRequestWithokhttp();
    return inflate;
  }


  private void initView(View inflate) {
    sysViewLayout = inflate.findViewById(R.id.ViewLayout);
    textView = inflate.findViewById(R.id.tv_sys_title);
    listView = inflate.findViewById(R.id.listview_sys);
    context = getContext();
  }

  private void initListener() {
  }

  private void sendRequestWithokhttp() {
    OkHttpClient mOkHttpClient = new OkHttpClient();
    Request request = new Request.Builder()
        .url("https://www.wanandroid.com/navi/json")
        .get()
        .build();
    Call call = mOkHttpClient.newCall(request);
    call.enqueue(new Callback() {
      @Override
      public void onFailure(@NotNull Call call, @NotNull IOException e) {
      }

      @Override
      public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
        if (response.isSuccessful()) {
          Gson gson = new Gson();
          NavOne navOne = gson.fromJson(response.body().string(), NavOne.class);
          getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
              refreshListView(navOne);
            }
          });
        }

      }

    });
  }

  private void refreshListView(NavOne navOne) {
    List<NavTwo> data = navOne.getData();
    flowAdapter.setData(data);
    flowAdapter.notifyDataSetChanged();
  }

  private void initSystemView() {
    flowAdapter = new FlowAdapter2(context, dataList);
    listView.setAdapter(flowAdapter);
  }

}